from typing import Sequence

from fastapi import APIRouter, Security, UploadFile
from fastapi.responses import StreamingResponse

from app.deps.authorize_deps import validate_permissions
from app.deps.base_deps import DynamicQueryDep, IdsQueryDep
from app.deps.controller_deps import DictControllerDep as ControllerDep
from app.models.base import IdType
from app.schemas.base_schema import AuthScopePrefixConst
from app.schemas.dict_schema import (
    DictOut as OutModel,
    DictUpdate as UpdateModel,
    DictCreate as CreateModel,
    DictExport as ExportModel
)
from app.schemas.result_schema import ResultModel, PageModel

dict_router = APIRouter()

SCOPE_PREFIX=AuthScopePrefixConst.DICT_SCOPE_PREFIX.value

@dict_router.get("/get_all", summary="获取所有",dependencies=[Security(validate_permissions, scopes=[f"{SCOPE_PREFIX}view"])])
async def get_all(query_in: DynamicQueryDep, controller: ControllerDep) -> ResultModel[Sequence[OutModel]]:
    return await controller.get_all( query_in=query_in)

@dict_router.get("/get_page/{current_page}/{page_size}", summary="分页获取",dependencies=[Security(validate_permissions, scopes=[f"{SCOPE_PREFIX}view"])])
async def get_page(current_page: int, page_size: int, query_in: DynamicQueryDep, controller: ControllerDep) -> ResultModel[PageModel[OutModel]]:
    return await controller.get_page(current_page=current_page, page_size=page_size, query_in=query_in)

@dict_router.get("/get_by_id/{id}", summary="根据ID获取",dependencies=[Security(validate_permissions, scopes=[f"{SCOPE_PREFIX}view"])])
async def get_by_id(id: IdType,controller: ControllerDep) -> ResultModel[OutModel]:
    return await controller.get_by_id(id=id)

@dict_router.get("/get_by_ids", summary="根据ID列表获取",dependencies=[Security(validate_permissions, scopes=[f"{SCOPE_PREFIX}view"])])
async def get_by_ids(ids:IdsQueryDep,controller: ControllerDep) -> ResultModel[Sequence[OutModel]]:
    return await controller.get_by_ids(ids=ids)

@dict_router.post("/export_template", summary="导出模版",dependencies=[Security(validate_permissions, scopes=[f"{SCOPE_PREFIX}export"])])
async def export_template(controller: ControllerDep) -> StreamingResponse:
    return await controller.export_template(CreateModel, export_name="字典模版")

@dict_router.post("/batch_export", summary="批量导出",dependencies=[Security(validate_permissions, scopes=[f"{SCOPE_PREFIX}export"])])
async def batch_export(ids:Sequence[IdType],controller: ControllerDep) -> StreamingResponse:
    return await controller.batch_export(ExportModel,ids=ids,export_name="字典数据")

@dict_router.post("/batch_import", summary="批量导入",dependencies=[Security(validate_permissions, scopes=[f"{SCOPE_PREFIX}import"])])
async def batch_import(file:UploadFile,controller: ControllerDep) -> ResultModel[bool]:
    return await controller.batch_import(model=CreateModel,file=file)

@dict_router.post("/create", summary="添加",dependencies=[Security(validate_permissions, scopes=[f"{SCOPE_PREFIX}add"])])
async def create(obj: CreateModel, controller: ControllerDep) -> ResultModel[bool]:
    return await controller.create(obj=obj)

@dict_router.post("/batch_create", summary="批量添加",dependencies=[Security(validate_permissions, scopes=[f"{SCOPE_PREFIX}add"])])
async def batch_create( objs: Sequence[CreateModel], controller: ControllerDep) -> ResultModel[bool]:
    return await controller.batch_create(objs=objs)

@dict_router.put("/update", summary="更新",dependencies=[Security(validate_permissions, scopes=[f"{SCOPE_PREFIX}update"])])
async def update(obj_new: UpdateModel,controller: ControllerDep) -> ResultModel[bool]:
    return await controller.update(obj=obj_new)

@dict_router.patch("/enable/{id}/{is_enabled}", summary="切换状态",dependencies=[Security(validate_permissions, scopes=[f"{SCOPE_PREFIX}update"])])
async def enable(id: IdType, is_enabled: bool,controller: ControllerDep) -> ResultModel[bool]:
    return await controller.update_field(id=id, field_name="is_enabled", field_value=is_enabled, model_name="切换状态")

@dict_router.delete("/delete/{id}", summary="删除",dependencies=[Security(validate_permissions, scopes=[f"{SCOPE_PREFIX}delete"])])
async def delete(id: IdType,controller: ControllerDep) -> ResultModel[bool]:
    return await controller.delete(id=id)

@dict_router.delete("/batch_delete", summary="批量删除",dependencies=[Security(validate_permissions, scopes=[f"{SCOPE_PREFIX}batch_delete"])])
async def batch_delete(ids: Sequence[IdType],controller: ControllerDep) -> ResultModel[bool]:
    return await controller.batch_delete(ids=ids)
